//
//  TabItem.m
//  BaiduTravel
//
//  Created by guantianpeng on 13-5-10.
//  Copyright (c) 2013年 Baidu.com. All rights reserved.
//

#import "TabItem.h"
#import "CommonDefines.h"
#import "UIColor-Expanded.h"
#import "ImageDefs.h"

static const CGFloat kLabelMarginLeft = 5;
static const CGFloat kLabelMarginRight = 5;
static const CGFloat kText2ArrowMargin = 10;
static const CGFloat kItemArrowWidth = 7;
static const CGFloat kItemArrowHeight = 4;
//static const CGFloat kItemArrowWidth = 14;
//static const CGFloat kItemArrowHeight = 12;
@interface TabItem(){
    NSInteger _selectedState;
}

@property (nonatomic,retain)  UIImageView *iVItemBg;
@property (nonatomic,retain)  UIImageView *iVItemArrow;
@property (nonatomic,retain)  UIImageView *iVItemBottomArrow;
@property (nonatomic,retain)  UILabel *lbTitle;

- (void)initImage;

- (void)initSubView;

- (void)onTap:(UIGestureRecognizer *)gesture;

@end


@implementation TabItem

@synthesize tabItemDelegate = _tabItemDelegate;
@synthesize itemSelected = _itemSelected;
@synthesize title = _title;
@synthesize iVItemBg = _iVItemBg;
@synthesize iVItemArrow  = _iVItemArrow;
@synthesize iVItemBottomArrow = _iVItemBottomArrow;
@synthesize lbTitle = _lbTitle;
@synthesize imgSelectedBg = _imgSelectedBg;
@synthesize imgNormalBg = _imgNormalBg;
@synthesize imgSelectedArrow = _imgSelectedArrow;
@synthesize imgNormalArrow = _imgNormalArrow;
@synthesize imgItemBottomArrow = _imgItemBottomArrow;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initImage];
        [self initSubView];
        
        [self addGestureRecognizer:[[[UITapGestureRecognizer alloc]
                                     initWithTarget:self
                                     action:@selector(onTap:)] autorelease]];
        self.itemSelected = NO;
    }
    
    return self;
}

- (void)initImage
{
    _imgNormalBg = [[UIColor createImageWithColor:[UIColor colorWithRed:228.0/255 green:224.0/255 blue:220.0/255 alpha:1]] retain];
//    _imgNormalBg = [[kItemBgNormal stretchableImageWithLeftCapWidth:0 topCapHeight:0]retain];
    _imgSelectedBg = [[kItemBgPressed stretchableImageWithLeftCapWidth:0 topCapHeight:0]retain];
    _imgNormalArrow = [kItemArrowNormal retain];
    _imgSelectedArrow = [kItemArrowPressed retain];
    _imgItemBottomArrow = [kTabItemBottomArrow retain];
}

- (void)initSubView
{
    _iVItemBg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    
    _lbTitle = [[UILabel alloc]initWithFrame:CGRectZero];
    _lbTitle.backgroundColor = [UIColor clearColor];
    _lbTitle.font = [UIFont fontWithName:kFontNameHelvetica size:14];
    _lbTitle.textColor = [UIColor colorWithRed:92.0/255 green:87.0/255 blue:88.0/255 alpha:1];
    _lbTitle.shadowOffset = CGSizeMake(0,1);
    _lbTitle.shadowColor = [UIColor colorWithHex:0XFFFFFFFF];
    _lbTitle.textAlignment = NSTextAlignmentCenter;
    
    _iVItemArrow = [[UIImageView alloc]initWithFrame:CGRectZero];
    _iVItemBottomArrow = [[UIImageView alloc]initWithFrame:CGRectMake((self.frame.size.width - _imgItemBottomArrow.size.width) / 2.0,self.frame.size.height - _imgItemBottomArrow.size.height, _imgItemBottomArrow.size.width, _imgItemBottomArrow.size.height)];
    [_iVItemBottomArrow setImage:_imgItemBottomArrow];
    
    [self addSubview:_iVItemBg];
    [self addSubview:_lbTitle];
    [self addSubview:_iVItemArrow];
    [self addSubview:_iVItemBottomArrow];
}

- (void)setTitle:(NSString *)title
{
    if (title == _title) {
        return;
    }
    [_title release];
    _title = [title retain];
    
    [_lbTitle setText:title];
    
    [self setNeedsLayout];
}

- (void)setItemSelected:(BOOL)itemSelected
{
    _itemSelected = itemSelected;
    if (itemSelected) {
        _selectedState = 1;
        
        [_iVItemBg setImage:_imgSelectedBg];
        [_iVItemArrow setImage:_imgSelectedArrow];
        _iVItemBottomArrow.hidden = NO;
//        [self bringSubviewToFront:_iVItemBottomArrow];
    }
    else {
        [_iVItemBg setImage:_imgNormalBg];
        [_iVItemArrow setImage:_imgNormalArrow];
        _iVItemBottomArrow.hidden = YES;
    }
    _iVItemArrow.transform = _iVItemBg.transform;

    [self setNeedsLayout];

}

- (void)setItemSelectedWithAnotherState
{
    if (_selectedState == 1) {
        _selectedState = 2;
    }else{
        _selectedState = 1;
    }

    if (_selectedState == 1) {
        [_iVItemBg setImage:_imgSelectedBg];
        [_iVItemArrow setImage:_imgSelectedArrow];
        _iVItemBottomArrow.hidden = NO;
        _iVItemArrow.transform = _iVItemBg.transform;
        //        [self bringSubviewToFront:_iVItemBottomArrow];
    }
    else {
        [_iVItemBg setImage:_imgSelectedBg];
        [_iVItemArrow setImage:_imgSelectedArrow];
        _iVItemArrow.transform =   CGAffineTransformMakeRotation(M_PI);
        _iVItemBottomArrow.hidden = NO;
    }
    
    [self setNeedsLayout];
    
}


- (void)setImgSelectedBg:(UIImage *)imgSelectedBg
{
    if (imgSelectedBg == _imgSelectedBg) {
        return;
    }
    [_imgSelectedBg release];
    _imgSelectedBg = [imgSelectedBg retain];
    [_iVItemBg setImage:imgSelectedBg];
}

-(void)setImgNormalBg:(UIImage *)imgNormalBg
{
    if (imgNormalBg == _imgNormalBg) {
        return;
    }
    [_imgNormalBg release];
    _imgNormalBg = [imgNormalBg retain];
    [_iVItemBg setImage:imgNormalBg];
}

-(void)setImgNormalArrow:(UIImage *)imgNormalArrow
{
    if (_imgNormalArrow == imgNormalArrow) {
        return;
    }
    [_imgNormalArrow release];
    _imgNormalArrow = [imgNormalArrow retain];
    [_iVItemArrow setImage:imgNormalArrow];
}

- (void)setImgSelectedArrow:(UIImage *)imgSelectedArrow
{
    if (_imgSelectedArrow == imgSelectedArrow) {
        return;
    }
    [_imgSelectedArrow release];
    _imgSelectedArrow = [imgSelectedArrow retain];
    [_iVItemArrow setImage:imgSelectedArrow];
}

- (void)setImgItemBottomArrow:(UIImage *)imgItemBottomArrow
{
    if (_imgItemBottomArrow == imgItemBottomArrow) {
        return;
    }
    
    [_imgItemBottomArrow release];
    _imgItemBottomArrow = [imgItemBottomArrow retain];
    [_iVItemBottomArrow setImage:imgItemBottomArrow];
}

-(void) setItemSelectedBackground
{
    [_iVItemBg setImage:_imgSelectedBg];
}

-(void) resetItemNormalBackground
{
    [_iVItemBg setImage:_imgNormalBg];
}

#pragma mark - gesture tap
- (void)onTap:(UIGestureRecognizer *)gesture {
    UITapGestureRecognizer *tapGesture = (UITapGestureRecognizer *)gesture;
    
   if (tapGesture.state == UIGestureRecognizerStateEnded) {
        
        // 判断网络状态
//        if ( kNotReachable == [self getNetWorkState]) {
//            [AppDelegate alertWithNetworkError];
//            return;
//        }
       if (![self itemSelected]) {
           [self setItemSelected:YES];
           for (UIView *subView in self.superview.subviews) {
               if ([subView isKindOfClass:[TabItem class]]) {
                   TabItem *item = (TabItem *)subView;
                   if (item != self) {
                       [item setItemSelected:NO];
                   }
               }
           }
       }else{
           [self setItemSelectedWithAnotherState];
//           [self setItemSelected:NO];
       }
       
        if ([self.tabItemDelegate respondsToSelector:@selector(didSelectTabItem:)]) {
            [self.tabItemDelegate didSelectTabItem:self];
        }
    }
}

#pragma mark - subviews layout
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGSize size = [_title sizeWithFont:_lbTitle.font constrainedToSize:CGSizeMake(80, 9999)];
    CGFloat contentW = size.width + kItemArrowWidth + kText2ArrowMargin;
    CGFloat x = (self.frame.size.width - contentW) / 2;
    CGFloat y = (self.frame.size.height - size.height) / 2;
    CGRect lblRect = CGRectMake(x, y, size.width, size.height);
    _lbTitle.frame = lblRect;
    
    // for center point
    x = lblRect.origin.x + lblRect.size.width + kText2ArrowMargin + kItemArrowWidth / 2;
    y = _lbTitle.center.y;
    _iVItemArrow.bounds = CGRectMake(0, 0, kItemArrowWidth, kItemArrowHeight);
    _iVItemArrow.center = CGPointMake(x, y);
    
//    static int timesCalled = 0;
//    NSLog(@"times layout called:%d", ++timesCalled);
}

- (void) layoutSubviewsWithArrowWidth:(CGFloat)width andHeight:(CGFloat)height iconInRight:(BOOL)inRight
{
    [super layoutSubviews];
    
    CGSize size = [_title sizeWithFont:_lbTitle.font constrainedToSize:CGSizeMake(80, 9999)];
    CGFloat contentW = size.width + width + kText2ArrowMargin;
    CGFloat x = (self.frame.size.width - contentW) / 2;
    CGFloat y = (self.frame.size.height - size.height) / 2;
    CGRect lblRect = CGRectMake(x, y, size.width, size.height);
    _lbTitle.frame = lblRect;
    
    // for center point
    if (inRight) {
        x = lblRect.origin.x + lblRect.size.width + kText2ArrowMargin + kItemArrowWidth / 2;
    }else{
        x = lblRect.origin.x - kText2ArrowMargin - kItemArrowWidth / 2;
    }
    y = _lbTitle.center.y;
    _iVItemArrow.bounds = CGRectMake(0, 0, width, height);
    _iVItemArrow.center = CGPointMake(x, y);
}

- (void)dealloc
{
    self.imgSelectedBg = nil;
    self.imgNormalBg = nil;
    self.imgNormalArrow = nil;
    self.imgSelectedArrow = nil;
    self.imgItemBottomArrow = nil;
    self.lbTitle = nil;
    self.iVItemBg = nil;
    self.iVItemArrow = nil;
    self.title = nil;
    self.iVItemBottomArrow = nil;
    self.tabItemDelegate = nil;
    
    [super dealloc];
}

@end
